package com.example.acharten;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Map.Entry;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.RangeCategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;

/**
 * 直方图
 * @author lxj
 *
 */
public class HistogramChart {
	//设置Activity标题
	private String activityTitle;
	private List<Map<String, Object>> dataContents; //数据内容
	private Integer xMax; //x最大值
	private Integer xMin; //x最小值
	private String title;//标题
	private Integer yMax; //y轴最大值
	private Integer yMin; //y轴最小值

	private String yTitle;//y轴标题
	private String xTitle;//x轴标题
	private int yLabels; //将轴均分为几等分
	
	private boolean backGroundColorFlag; //是否允许设置背景色
	private int backGroundColor;//背景颜色
	private String[] keys;
	
	protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) {

		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();

		renderer.setAxisTitleTextSize(16);
		renderer.setChartTitleTextSize(20);
		renderer.setLabelsTextSize(15);
		renderer.setLegendTextSize(15);
		int length = colors.length;
		for (int i = 0; i < length; i++) {
			SimpleSeriesRenderer r = new SimpleSeriesRenderer();
			r.setColor(colors[i]);
			renderer.addSeriesRenderer(r);
		}
		return renderer;
	}

	protected void setChartSettings(XYMultipleSeriesRenderer renderer,
			String title, String xTitle, String yTitle, double xMin,
			double xMax, double yMin, double yMax, int axesColor,
			int labelsColor) {
		renderer.setChartTitle(title);
		renderer.setXTitle(xTitle);
		renderer.setYTitle(yTitle);
		renderer.setXAxisMin(xMin);
		renderer.setXAxisMax(xMax);
		renderer.setYAxisMin(yMin);
		renderer.setYAxisMax(yMax);
		renderer.setAxesColor(axesColor);
		renderer.setLabelsColor(labelsColor);
	}
	
	public XYMultipleSeriesRenderer getRenderer(List<Map<String, Object>> dataContents) {
		int[] colors = new int[] { Color.RED };
//		Random random = new Random();  
//		int[] colors = new int[keys.length];
//		
//		for (int i = 0; i < colors.length; i++) {
//			colors[i] = Color.rgb(random.nextInt()*128+128 ,
//					random.nextInt()*128+128, random.nextInt()*128+128);
//		}
		XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
		/**
		 * 设置背景颜色
		 */
		if (backGroundColorFlag == true) {
			renderer.setApplyBackgroundColor(true);
			renderer.setBackgroundColor(backGroundColor);
		}
		
		setChartSettings(renderer, title , xTitle, yTitle, 0.5, dataContents.size() + 0.5,
				0, yMax,Color.GRAY, Color.GREEN);
		renderer.setBarSpacing(0.5);
		renderer.setAxesColor(Color.BLUE);
		renderer.setXLabels(0);
		renderer.setYLabels(yLabels);
		//设置x轴数据
		for (int i = 0; i < keys.length; i++) {
			renderer.addXTextLabel(i+1, keys[i]);
		}
		SimpleSeriesRenderer r = renderer.getSeriesRendererAt(0);
		r.setDisplayChartValues(true);
		r.setChartValuesTextSize(12);
		r.setChartValuesSpacing(2);
		r.setGradientEnabled(true);
		r.setGradientStart(-20, Color.BLUE);
		r.setGradientStop(100, Color.GREEN);
		//缩放设置
		renderer.setZoomButtonsVisible(true);
		renderer.setZoomEnabled(true);
		
		return renderer;
	}
	
	public XYMultipleSeriesDataset getDataset(List<Map<String, Object>> dataContents) {
		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		RangeCategorySeries series = new RangeCategorySeries(activityTitle);
		String key = "";
		keys = new String[dataContents.size()];
		//y轴数据设置
		for (int i = 0; i < dataContents.size(); i++) {
			Map<String, Object> dataContent = dataContents.get(i);
			
			Iterator<Entry<String, Object>> it = dataContent.entrySet().iterator();
			
			while (it.hasNext()) {
				Entry<String, Object> content = it.next();
				key = content.getKey();
				series.add(0,Double.parseDouble(content.getValue().toString()));
			}
			
			if (key != null && !"".equals(key)) {
				keys[i] = key;
			}
		}	
		
		dataset.addSeries(series.toXYSeries());
		return dataset;
	}
	
	public Intent createChartIntent(Context context,List<Map<String, Object>> dataContents,
			String activityTitle, String title,String  xTitle, String yTitle,Integer yMax, Integer yMin, int yLabels, 
			boolean backGroundColorFlag, int backGroundColor) {
		this.dataContents = dataContents;
		this.title = title;
		this.activityTitle = activityTitle;
		this.yTitle = yTitle;
		this.xTitle = xTitle;
		this.yMax = yMax;
		this.yMin = yMin;
		this.yLabels = yLabels;
		this.backGroundColorFlag = backGroundColorFlag;
		this.backGroundColor = backGroundColor;
		
		Intent intent = ChartFactory.getRangeBarChartIntent(context, getDataset(dataContents),
				getRenderer(dataContents), Type.STACKED, activityTitle);
		return intent;
	}
	
	
	public String getActivityTitle() {
		return activityTitle;
	}

	public void setActivityTitle(String activityTitle) {
		this.activityTitle = activityTitle;
	}
	
	public List<Map<String, Object>> getDataContents() {
		return dataContents;
	}

	public void setDataContents(List<Map<String, Object>> dataContents) {
		this.dataContents = dataContents;
	}

	public Integer getyMax() {
		return yMax;
	}

	public void setyMax(Integer yMax) {
		this.yMax = yMax;
	}

	public Integer getxMax() {
		return xMax;
	}

	public void setxMax(Integer xMax) {
		this.xMax = xMax;
	}

	public Integer getxMin() {
		return xMin;
	}

	public void setxMin(Integer xMin) {
		this.xMin = xMin;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Integer getyMin() {
		return yMin;
	}

	public void setyMin(Integer yMin) {
		this.yMin = yMin;
	}

	public String getyTitle() {
		return yTitle;
	}

	public void setyTitle(String yTitle) {
		this.yTitle = yTitle;
	}

	public String getxTitle() {
		return xTitle;
	}

	public void setxTitle(String xTitle) {
		this.xTitle = xTitle;
	}

	public boolean isBackGroundColorFlag() {
		return backGroundColorFlag;
	}

	public void setBackGroundColorFlag(boolean backGroundColorFlag) {
		this.backGroundColorFlag = backGroundColorFlag;
	}

	public int getBackGroundColor() {
		return backGroundColor;
	}

	public void setBackGroundColor(int backGroundColor) {
		this.backGroundColor = backGroundColor;
	}

	public String[] getKeys() {
		return keys;
	}

	public void setKeys(String[] keys) {
		this.keys = keys;
	}

	public int getyLabels() {
		return yLabels;
	}

	public void setyLabels(int yLabels) {
		this.yLabels = yLabels;
	}
	
}
